﻿using System;
using System.Collections.Generic;
using System.Text;
using OpenTK;
using System.Drawing;

using OpenTK.Graphics.OpenGL;
using OpenTK.Input;

using Xeno.Core;
using Xeno.Module;
using OpenTK;
namespace Xeno.GUI
{
    public class XuiButton : XuiWidget
    {
        public string Text { get; set; }
        public XenoText.XenoTextCache TextCache { get; private set; }
        public XuiButton(Vector2 size,string text)
            : base(size)
        {
            Text = text;
            TextCache = XenoText.CreateText(Text);
        }
        public bool Over { get; private set; }
        public bool Pushed { get; private set; }
        public override void DoUpdate()
        {
            float mx, my;
            mx = XenoInput.MouseX();
            my = XenoInput.MouseY();
            bool mb = XenoInput.IsMouseDown(MouseButton.Left);

            Vector3 wp = Attached.WorldPosition;

            Over = false;
            Pushed = false;

            if (mx >= wp.X && mx <= wp.X + Size.X && my >= wp.Y && my <= wp.Y + Size.Y)
            {
                if (mb)
                {
                    Pushed = true;
                }
                Over = true;
            }

            base.DoUpdate();
        }
        public override void DoRender()
        {

            Begin2D();

            Vector3 wp = Attached.WorldPosition;


            if (Pushed)
            {
                GL.Color4(Color.White);
            }
            else if (Over)
            {
                GL.Color4(Color.DarkBlue);
            }else
            {
                GL.Color4(Color.Azure);
            }
            GL.Begin(BeginMode.Quads);

            GL.Vertex2(wp.X, wp.Y);
            GL.Vertex2(wp.X + Size.X, wp.Y);

            GL.Color4(Color.DarkSlateBlue);

            GL.Vertex2(wp.X + Size.X, wp.Y + Size.Y);
            GL.Vertex2(wp.X, wp.Y + Size.Y);


            GL.End();

            GL.LineWidth(1);
            GL.Begin(BeginMode.Lines);

            GL.Color4(Color.DarkSlateGray);

            GL.Vertex2(wp.X, wp.Y);
            GL.Vertex2(wp.X, wp.Y + Size.Y);
            GL.Vertex2(wp.X, wp.Y);
            GL.Vertex2(wp.X + Size.X, wp.Y);


            GL.End();

            float tx = wp.X + Size.X / 2 - (TextCache.Width / 2);
            float ty = wp.Y + Size.Y / 2 - (TextCache.Height / 4);

            End2D();

            XenoText.DrawText(TextCache, (int)tx, (int)ty,Color.Black);

            
        }
    }
}
